package com.nd.android.u.ui.longClickMenu.messageList;

import ims.utils.CommUtil;
import android.content.Context;
import android.text.ClipboardManager;
import android.text.SpannableString;

import com.common.android.utils.smiley.Smiley;
import com.common.android.utils.smiley.Smileyhelper;
import com.nd.android.u.controller.ChatConst.LocalMessageType;
import com.nd.android.u.controller.ChatConst.MessageContentType;
import com.nd.android.u.controller.bean.ImageMessage;
import com.nd.android.u.controller.innerInterface.IChatListLongClickMenu;
import com.nd.android.u.controller.innerInterface.IMessageDisplay;
import com.nd.android.u.ui.R;
import com.product.android.business.ApplicationVariable;

/**
 * [复制] 菜单项
 * @author yufeng
 * @version 创建时间：2014-7-18 上午10:40:44
 */
@SuppressWarnings("deprecation")
public class MenuItemCopy implements IChatListLongClickMenu {

//    private Context mContext;
    
    private IMessageDisplay mMessage;
    
    @Override
    public String getLabel() {
        return ApplicationVariable.INSTANCE.applicationContext.getString(R.string.context_menu_item_copy);
    }
    
    @Override
    public void onClick(Context context) {
        ClipboardManager cm = (ClipboardManager) context
                .getSystemService(android.content.Context.CLIPBOARD_SERVICE);
        String text = getCopyTextContent(mMessage);
        cm.setText(text);
    }

    
    /**
     * 获取复制的内容
     * <br>Created 2014-7-25 下午5:39:02
     * @param message BaseDisplayMessage 对象
     * @return 复制的内容
     * @author       yufeng
     */
    private String getCopyTextContent(IMessageDisplay message) {
        
        switch (message.getMessageContentType()) {
        case MessageContentType.VIDEO:
            return ApplicationVariable.INSTANCE.applicationContext.getString(R.string.chat_vidio);
            
        case MessageContentType.AUDIO:
        case MessageContentType.OLD_AUDIO:
            return ApplicationVariable.INSTANCE.applicationContext.getString(R.string.chat_audio);
            
        case MessageContentType.FILE:
        case MessageContentType.OLD_FILE:
            return ApplicationVariable.INSTANCE.applicationContext.getString(R.string.chat_file);

        default:
            break;
        }
        String textStr = "";
        if (message.getDisplayDatas().size()==0){
        	return message.getDisplayContent();
        }
        for (Object object :  message.getDisplayDatas()) {
            if (object instanceof String) {
                textStr += object;
            } else if (object instanceof SpannableString) {
                textStr += object.toString();
            } else if (object instanceof ImageMessage) {
                textStr += Smiley.IMGSTART + ((ImageMessage) object).getImgurl() + Smiley.IMGEND;
            }
        }
        return textStr;
    }
    
    @Override
    public boolean isEnable(IMessageDisplay message) {
    	mMessage=message;
        // 系统消息和应用消息没有弹出菜单, 公众号消息只有"删除"项
        if (mMessage.getMessageType() == LocalMessageType.SYSTEM || mMessage.getMessageType() == LocalMessageType.APP
                || mMessage.getMessageType() == LocalMessageType.PUBLIC_NUMBER) {
            return false;
        }
        return isCopyableMessage(mMessage);
    }
    
    /**
     * 判断消息是否可复制
     * <br>Created 2014-7-17 下午5:17:21
     * @param msg 消息对象
     * @return 是否可以被复制
     * @author       yufeng
     */
    private boolean isCopyableMessage(IMessageDisplay msg){
        int messageContentType=msg.getMessageContentType();
        if (messageContentType == MessageContentType.AUDIO
                || messageContentType == MessageContentType.OLD_AUDIO
                || messageContentType == MessageContentType.FILE
                || messageContentType== MessageContentType.OLD_FILE) {
            
            // 语音信息、文件信息不能复制
            return false;
        }
        
        //这边可以直接用message.containImage方法？
        return !containImage();
        
//        for (Object o : msg.getDisplayDatas()) {
//            if (o instanceof ImageMessage) {
//                if (((ImageMessage) o).getType() != ImageMessage.SMILY_TYPE) {
//                    // 带图片的信息且图片不全为表情
//                    return false;
//                }
//            }
//        }
//        return true;
    }
    
    /**
     * 消息中是否包含有图片
     * @param list
     * @return
     */
    private boolean containImage() {
        if (mMessage.getDisplayDatas().isEmpty()) {
            return false;
        }

        for (Object o : this.mMessage.getDisplayDatas()) {
            if (o instanceof ImageMessage) {
                ImageMessage imageMsg = (ImageMessage) o;
                if (imageMsg.getType() == ImageMessage.CUSTOM_TYPE || imageMsg.getType() == ImageMessage.IMAGE_TYPE) {
                    return true;
                }else{
                	int id=CommUtil.parseInt(imageMsg.getImgurl());
                	if (Smileyhelper.getInstance().isCatID(id)){
                		return true;
                	}
                }
            }
        }
        return false;
    }
}
